﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business.IManager;
using Business.Manager;
using Entity;
using Utility;
using System.Data;
using System.Collections;
namespace OnlinePayment.OP
{
    public partial class PayTuitionView : System.Web.UI.Page
    {
        
        IPaymentProjectManager projectManager = new PaymentProjectManager();
        IPaymentItemManager itemManager = new PaymentItemManager();
        IOrderManager orderManager = new OrderManager();
        public List<int> unPaidYearList = new List<int>();
        public DataTable tuitionDataTable = new DataTable();


        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["User"] == null)
            {
                //student = (Student)Session["User"];
                //Response.Redirect("/Account/LogIn.aspx");
            }
            else
            {
                Student stu = (Student)Session["User"];
              
                //if (!IsPostBack)
                //{
                    initUnPaidYearList(stu.StudentId.ToString());
                    initTuitionTable(itemManager.getStudentUnPaidDetailItems(stu.StudentId).Tables[0]);
                    //tuitionDataTable = 
                    //GV_PayTuition.DataSource = itemManager.getStudentUnPaidDetailItems("123012010196");
                    //GV_PayTuition.DataBind();
                    //DL_PayTuition.DataSource = unPaidYearList;
                   // DL_PayTuition.DataBind();
               // }
            }

        }


        private void initTuitionTable(DataTable dt)
        {
            tuitionDataTable.Columns.Add("CheckBox");
            tuitionDataTable.Columns.Add("detailName");
            tuitionDataTable.Columns.Add("amountFcPay");
            tuitionDataTable.Columns.Add("amountShPay");
            tuitionDataTable.Columns.Add("detailStatus");
            tuitionDataTable.Columns.Add("curPayAccount");
            tuitionDataTable.Columns.Add("year");
            tuitionDataTable.Columns.Add("detailId");

            DataRow row;
            for (int i = 0; i < dt.Rows.Count;i++ )
            {
                row = tuitionDataTable.NewRow();
                row[0] = false;
                row[1] = dt.Rows[i]["detailName"].ToString();
                row[2] = Convert.ToInt32(dt.Rows[i]["amountFcPay"]);
                row[3] = Convert.ToInt32(dt.Rows[i]["amountShPay"]); 
                row[4] = dt.Rows[i]["detailStatus"].ToString();
                row[5] = 0;
                row[6] = Convert.ToInt32(dt.Rows[i]["year"]);
                row[7] = Convert.ToInt32(dt.Rows[i]["detailId"]); 
                tuitionDataTable.Rows.Add(row);
            }
            //tuitionTableValue.Value = tuitionDataTable;
        }


        private void initUnPaidYearList(String stuId)
        {
            DataSet ds = itemManager.getStudentUnPaidDetailItems(stuId);
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                int year = Convert.ToInt32(row["year"]);
                if (!unPaidYearList.Contains(year))
                {
                    unPaidYearList.Add(year);
                }
            }
            unPaidYearList.Sort();

        }

        protected void DL_PayTuition_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            //string orderId = this.DL_PayTuition.DataKeys[e.Item.ItemIndex].ToString();

            GridView gv = (GridView)(e.Item.FindControl("GV_PayTuition"));
            int year = (int)e.Item.DataItem;
            gv.DataSource = itemManager.getStudentUnPaidDetailItems("123012010196");
            gv.DataBind();
        }


        protected void btn_Pay_Click(object sender, EventArgs e)
        {
            String[] chekedIdArray = Request.Form.GetValues("checkbox");
            String[] inputTextArray = Request.Form.GetValues("txtCurAmount");
            //Hashtable htPayDetail = new Hashtable();

            List<ProjectOrder> orders = new List<ProjectOrder>();
            //List<OrderDetail> details = new List<OrderDetail>();
            //int orderId = orderManager.getOrderId(String.Empty);
            List<int> stuProjectIdList = new List<int>();

            for (int i = 0; i < chekedIdArray.Length; i++)
            {

                String dvalue = chekedIdArray[i];   //详细项编号
                String curAmount = inputTextArray[i];   //对应的缴费金额
                string stuId = (Master.Master.FindControl("lblStudentId") as Label).Text;
                //string stuId = "123012010196";
                int stuProjectId = itemManager.getStudentProjectIdByDetailId(Convert.ToInt32(dvalue), stuId);
                if (!stuProjectIdList.Contains(stuProjectId))
                {
                    stuProjectIdList.Add(stuProjectId);
                    ProjectOrder order = new ProjectOrder();
                    List<OrderDetail> details = new List<OrderDetail>();
                    OrderDetail detail = new OrderDetail();
                    order.PaymentType = "学费";
                    order.ProjectId = stuProjectId;
                    detail.Amount = Convert.ToDecimal(curAmount);
                    detail.DetailId = Convert.ToInt32(dvalue);
                    detail.ProjectId = stuProjectId;
                    order.TotalAmount = order.TotalAmount + detail.Amount;
                    details.Add(detail);
                    order.OrderDetail = details;
                    orders.Add(order);
                }
                else
                {
                    foreach (ProjectOrder po in orders)
                    {
                        if (po.ProjectId.Equals(stuProjectId))
                        {
                            OrderDetail detail = new OrderDetail();
                            detail.Amount = Convert.ToDecimal(curAmount);
                            detail.DetailId = Convert.ToInt32(dvalue);
                            detail.ProjectId = stuProjectId;
                            po.TotalAmount = po.TotalAmount + detail.Amount;
                            po.OrderDetail.Add(detail);
                            break;
                        }
                    }
                }
            } //end of for
            Session["Orders"] = orders;
            Response.Redirect("OrderConfirmView.aspx");
        }






    }
}